home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / position.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  5.0 KB  |  243 lines

  1.  
  2. /* Position reporting-related user commands (derived from RIP)
  3.  *   (c) 1993 Brian A. Lantz
  4.  */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "netuser.h"
  9. #include "internet.h"
  10. #include "cmdparse.h"
  11. #include "timer.h"
  12. #include "iface.h"
  13. #include "udp.h"
  14. #include "rip.h"
  15. #include "commands.h"
  16. #include "gps.h"
  17.  
  18. static int32 PosHost = -1;
  19. char *CurrentPos;
  20. static struct udp_cb *GPS_cb;
  21. static struct timer POStimer;
  22.  
  23. static int docurrent __ARGS((int argc,char *argv[],void *p));
  24. static int dohostname __ARGS((int argc,char *argv[],void *p));
  25. static int dolist __ARGS((int argc,char *argv[],void *p));
  26. static int dotimer __ARGS((int argc,char *argv[],void *p));
  27. void POStick __ARGS((void *v));
  28. static void gps_rx __ARGS((struct iface *iface,struct udp_cb *sock,int cnt));
  29.  
  30.  
  31. /* Start Position agent listening at local GPS UDP port */
  32. int
  33. POSstart()
  34. {
  35. struct socket lsock;
  36.  
  37.     lsock.address = INADDR_ANY;
  38.     lsock.port = IPPORT_GPS;
  39.  
  40.     if(GPS_cb == NULLUDP)
  41.         GPS_cb = open_udp(&lsock,gps_rx);
  42.  
  43.     return 0;
  44. }
  45.  
  46. int
  47. POSstop(argc,argv,p)
  48. int argc;
  49. char *argv[];
  50. void *p;
  51. {
  52.     del_udp(GPS_cb);
  53.     GPS_cb = NULLUDP;
  54.     return 0;
  55. }
  56.  
  57.  
  58. static struct cmds Positioncmds[] = {
  59.     "current",    docurrent,    0,    0,    NULLCHAR,
  60.     "hostname",    dohostname,    0,    0,    NULLCHAR,
  61.     "kick",        doPOSkick,    0,    0,    NULLCHAR,
  62.     "list",        dolist,        0,    0,    NULLCHAR,
  63.     "timer",    dotimer,    0,    0,    NULLCHAR,
  64.     NULLCHAR,
  65. };
  66.  
  67. int
  68. doposition(argc,argv,p)
  69. int argc;
  70. char *argv[];
  71. void *p;
  72. {
  73.     return subcmd(Positioncmds,argc,argv,p);
  74. }
  75.  
  76. /* Set the host that we are to report position data to */
  77. static int
  78. dohostname(argc,argv,p)
  79. int argc;
  80. char *argv[];
  81. void *p;
  82. {
  83.     if (argc > 1)
  84.         PosHost = resolve(argv[1]);
  85.     tprintf ("Position reporting to: %s\n", inet_ntoa(PosHost));
  86.     return 0;
  87. }
  88.  
  89. /* Display current position data string */
  90. int
  91. docurrent(argc,argv,p)
  92. int argc;
  93. char *argv[];
  94. void *p;
  95. {
  96. char *cp;
  97.  
  98.     if (!CurrentPos || !*CurrentPos)
  99.         cp = "unknown!\n";
  100.     else
  101.         cp = CurrentPos;
  102.     tprintf ("Current GPS Position data:\n%s\n", cp);
  103.     return 0;
  104. }
  105.  
  106. static int
  107. doPOSkick(argc,argv,p)
  108. int argc;
  109. char *argv[];
  110. void *p;
  111. {
  112.     POStick (NULL);
  113.     return 0;
  114. }
  115.  
  116. static int
  117. dolist(argc,argv,p)
  118. int argc;
  119. char *argv[];
  120. void *p;
  121. {
  122. char buf[GPSMAXLEN + 1], *cp;
  123. FILE *fp;
  124.  
  125.     fp = fopen (GPSfile, "r");    /* no error checking yet */
  126.     if (fp)        {
  127.         tprintf ("Current GPS Data:\n\n");
  128. /*        rewind (fp);        */
  129.         while (!feof (fp))    {
  130.             fread (buf, 1, GPSMAXLEN, fp);
  131.             if (feof (fp))
  132.                 continue;
  133. #ifdef nope
  134. #ifdef TNOS_68K
  135.             cp = strpbrk (buf, "\n\l");    /* no error chk */
  136. #else
  137.             cp = strpbrk (buf, "\r\n");    /* no error chk */
  138. #endif
  139.             *(++cp) = 0;
  140. #endif
  141.             cp = strchr (buf, '*'); /* find beginning of checksum */
  142.             *cp = 0;
  143.             tprintf ("%s\n", buf);
  144.         }
  145.         fclose (fp);
  146.     }
  147.     return 0;
  148. }
  149.  
  150. static int
  151. dotimer(argc,argv,p)
  152. int argc;
  153. char *argv[];
  154. void *p;
  155. {
  156.     if(argc < 2){
  157.         tprintf("Outgoing Position timer: %lu/%lu\n",
  158.             read_timer(&POStimer)/1000L,
  159.             dur_timer(&POStimer)/1000L);
  160.         return 0;
  161.     }
  162.     stop_timer (&POStimer);    /* just in case */
  163.     POStimer.func = (void (*)())POStick;/* what to call on timeout */
  164.     POStimer.arg = NULL;        /* dummy value */
  165.     set_timer(&POStimer,atol(argv[1])*1000L); /* set timer duration */
  166.     start_timer(&POStimer);     /* fire it up */
  167.     return 0;
  168. }
  169.  
  170. void
  171. POStick (v)
  172. void *v;
  173. {
  174. #ifdef nope
  175.     if (GPSactive != NULLIF)        {    /* GPS must be started */
  176.         psignal (GPSactive, 0);    /* wake up the GPS daemon */
  177.         pwait (CurrentPos);    /* wait until it's update is complete */
  178.     }
  179. #endif
  180.     if (CurrentPos && *CurrentPos)    /* not if nothing there */
  181.         gps_tx();
  182.     start_timer(&POStimer);     /* fire it up */
  183. }
  184.  
  185. /* Process GPS input received from 'interface'. */
  186. static void
  187. gps_rx(iface,sock,cnt)
  188. struct iface *iface;
  189. struct udp_cb *sock;
  190. int cnt;
  191. {
  192. struct mbuf *bp;
  193. struct socket fsock;
  194. char buf[GPSMAXLEN + 1], *cp = buf;
  195. char inbuf[GPSMAXLEN + 1];
  196. FILE *fp;
  197. int hostnamelen;
  198.  
  199.     /* receive the RIP packet */
  200.     recv_udp(sock,&fsock,&bp);
  201.  
  202.     memset (buf, 0x20, GPSMAXLEN);
  203.     while(len_p(bp))
  204.         *cp++ = PULLCHAR(&bp);
  205.     if (!(cp = strchr (buf, ':')))    /* not a valid gps frame */
  206.         return;
  207.     hostnamelen = (int) (cp - buf + 1);    /* include the ":" */
  208.     fp = fopen (GPSfile, "a+");    /* no error checking yet */
  209.     fseek (fp, GPSMAXLEN, 0);    /* don't touch 1st entry */
  210.     while (!feof (fp))    {
  211.         fread (inbuf, 1, GPSMAXLEN, fp);
  212.         if (!strncmp (inbuf, buf, hostnamelen))        { /* found it */
  213.             fseek (fp, (long) (ftell (fp) - (long) GPSMAXLEN), 0L);
  214.             break;
  215.         }
  216.     }
  217.     fwrite (buf, 1, GPSMAXLEN, fp);
  218.     fclose (fp);
  219.     return;        
  220. }
  221.  
  222. /* Send a GPS packet to the specified destination */
  223. static int
  224. gps_tx()
  225. {
  226. struct mbuf *bp;
  227. struct socket lsock,fsock;
  228.  
  229.     lsock.address = INADDR_ANY;
  230.     fsock.address = PosHost;
  231.     lsock.port = fsock.port = IPPORT_GPS;
  232.  
  233.     /* Send out one GPS packet as a broadcast to 'dest'  */
  234.     if((bp = alloc_mbuf(GPSMAXLEN)) == NULLBUF)
  235.         return -1;
  236.  
  237.     strncpy (bp->data, CurrentPos, GPSMAXLEN);
  238.     bp->cnt = strlen (bp->data);
  239.     send_udp(&lsock, &fsock,0,0,bp,bp->cnt,0,0);
  240.     return 0;
  241. }
  242.  
  243.